package mipo.top.pet.Service.impl;

import mipo.top.base.exception.BusinessException;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.base.util.BaiduAuditUtils;
import mipo.top.pet.Service.IProductService;
import mipo.top.pet.Service.IProductonlineAuditLogService;
import mipo.top.pet.domain.ProductonlineAuditLog;
import mipo.top.pet.domain.Product;
import mipo.top.pet.domain.ProductDetail;
import mipo.top.pet.mapper.ProductDetailMapper;
import mipo.top.pet.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServiceimpl extends BaseServiceimpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private IProductonlineAuditLogService productonlineAuditLogService;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.save(detail);
    }

    @Override
    public void update(Product product) {
        if(product.getResources()==""){
            product.setResources(null);
        }
        super.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.update(detail);
    }

    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        StringBuffer buffer = new StringBuffer();
        Map<Long, Product> map = super.findall().stream().filter(product -> product.getState() == 0).collect(Collectors.toMap(Product::getId, p -> p));
        ids.forEach(id->{
            Product product = map.get(id);
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                buffer.append(product.getName()+"的百度审核文本未通过");
            }
            String[] strings = product.getResources().split(",");
            for (int i = 0; i < strings.length; i++) {
                if (!BaiduAuditUtils.ImgCensor(strings[i])){
                    buffer.append(product.getName()+"的百度审核图片审核未通过,未通过图片为第"+i+"张");
                }
            }
            if(buffer.length()>0) {
                ProductonlineAuditLog productonlineAuditLog = new ProductonlineAuditLog();
                productonlineAuditLog.setAuditTime(date);
                productonlineAuditLog.setProductId(product.getId());
                productonlineAuditLog.setNote(buffer.toString());
                productonlineAuditLog.setState(0);
                productonlineAuditLogService.save(productonlineAuditLog);
                throw new BusinessException(buffer.toString());
            }
            product.setState(1);
            product.setOnsaletime(date);
            productMapper.update(product);

        });
    }

    @Override
    public Product idfindPetService(Long id) {
        return productMapper.idfindPetService(id);
    }
}
